<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:05
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\model\mysql;

use app\common\lib\Show;
use app\common\lib\Status;

class Theatre extends Base
{
    protected function getStartTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    protected function getEndTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * 查询剧院
     * @param $value
     * @return Base|array|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getGetVenueAttr($value)
    {
        $venue = (new Venue())->getBaseById((int)$value);
        if (!$venue) {
            return [];
        }
        $venue = $venue->toArray();
        return $venue;
    }

    /**
     * 剧场
     * @param $value
     * @return Base|array|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGetTheatreCateIdAttr($value)
    {
        $cate = (new TheatreCate())->getBaseById((int)$value);
        return $cate->toArray() ?? [];
    }

    /**
     * 厅位
     * @param $value
     * @return Base|array|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGetSeatIdAttr($value)
    {
        $cate = (new Seat())->getBaseById((int)$value);
        return $cate ?? [];
    }

    /**
     * 查询演员
     * @param $value
     * @return Base|array|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getGetPerformerAttr($value)
    {
        $ids = explode(',', $value);
        $performer = (new Performer())->getBaseByIdsValue($ids);
        return $performer ?? [];
    }

    /**
     * @param $value
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getGetPerformerNameAttr($value)
    {
        $ids = explode(',', $value);
        $performer = (new Performer())->getBaseByIdsValue($ids);
        return array_column($performer, 'title');
    }

    /**
     * 根据剧院查询所有剧场
     * @param array $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTheatreByVenueId(array $where = [], string $field = '*'): array
    {
        return $this->field($field)
            ->where($where)
            ->where('status', Status::mysql_normal)
            ->order('start_time asc,end_time asc')
            ->select()
            ->toArray();
    }

    protected function getRootPathThumbAttr($value)
    {
        return (string)url("/" . $value, [], false, true);
    }

    protected function getGetSTimeAttr($value)
    {
        return date('H:i', $value);
    }

    protected function getGetETimeAttr($value)
    {
        return date('H:i', $value);
    }

    protected function getGetVenueAddressAttr($value)
    {
        //先获取剧场
        $cate = (new TheatreCate())->getBaseById((int)$value);
        if (!$cate) {
            return "";
        }
        $venue = (new Venue())->getBaseById($cate['venue'], '*');
//        return $venue;
        if (!$venue) {
            return "";
        }
        return $venue;
    }

    public function getGetPerformerHeadAttr($value)
    {
        $performer = (new Performer())->getPerformerHead($value);
        $imgs = array_column($performer, 'head');
        if (!empty($imgs)) {
            $lists = [];
            foreach ($imgs as $v) {
                $lists[] = (string)url("/" . $v, [], false, true);
            }
            return $lists;
        } else {
            return [];
        }
    }

    /**
     * @param $value
     * @return false|string
     */
    protected function getStartDayAttr($value)
    {
        return date('Y-m-d', $value);
    }
    protected function getStartDayTextAttr($value)
    {
        return date('m-d', $value);
    }
    protected function getStartDayTimeAttr($value)
    {
        return date('H:i', $value);
    }

    protected function getEndDayTimeAttr($value)
    {
        return date('H:i', $value);
    }

    protected function getStartDateAAttr($value)
    {
        $date = date('a', $value);
        if ($date == 'am') {
            return "上午场";
        } else {
            return "下午场";
        }
    }
}